/**
 * <p>Original Author: Daniel Freeman    http://www.danielfreeman.asia</p>
 *
 * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:</p>
 *
 * <p>The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.</p>
 *
 * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS' OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.</p>
 *
 * <p>Licensed under The MIT License</p>
 * <p>Redistributions of files must retain the above copyright notice.</p>
 * <p>You are permitted to modify and enhance this code,</p>
 * <p>As a courtesy, please credit the original author, Daniel Freeman   http://www.danielfreeman.asia</p>
 */

// Rotation

package
{
	import com.adobe.utils.AGALMiniAssembler;
	
	import flash.display.Sprite;
	import flash.display.Stage3D;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	
	public class GreenSquare4 extends Sprite {
		
		[SWF(frameRate="60")]
		
		
		protected static const VERTICES:Vector.<Number> = 
			
			Vector.<Number> ([
			//	X,		Y,		Z
				-0.5,	-0.5,	0.0,	
				0.5,	-0.5,	0.0,
				0.5,	0.5,	0.0,
				-0.5,	0.5,	0.0,
			]);
		
		protected static const N:int = 3;
		
		protected static const INDICES:Vector.<uint> = Vector.<uint> ([ 0, 1, 2,	0, 2, 3 ]);
		
		
		protected var _context3D:Context3D;						// 3D graphics window on the stage
		protected var _shaderProgram:Program3D;					// Compiled shaders
		protected var _vertexBuffer:VertexBuffer3D;				// Vertexes used by our mesh
		protected var _indexBuffer:IndexBuffer3D;				// Indexes of each vertex of the mesh
		protected var _transform:Matrix3D = new Matrix3D();
		
		
		public function GreenSquare4(screen:Sprite = null) {
			if (screen)
				screen.addChild(this);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, onContext3DCreate);
			stage.stage3Ds[0].requestContext3D();
		}
		
		
		protected function onContext3DCreate(event:Event):void {				
			_context3D = Stage3D(event.currentTarget).context3D;
			
			if (!_context3D)
				return;
			
			stage.stage3Ds[0].removeEventListener(Event.CONTEXT3D_CREATE, onContext3DCreate);
			
			_context3D.enableErrorChecking = true;
			_context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 0, false);
			
			_vertexBuffer = _context3D.createVertexBuffer( VERTICES.length/N, N); 
			_vertexBuffer.uploadFromVector(VERTICES, 0, VERTICES.length/N);
			_context3D.setVertexBufferAt( 0, _vertexBuffer,  0, Context3DVertexBufferFormat.FLOAT_3 ); //va0
			
			_indexBuffer = _context3D.createIndexBuffer( INDICES.length );
			_indexBuffer.uploadFromVector(INDICES, 0, INDICES.length );
			
			var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
			vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
				"m44 op, va0, vc0"						// vertex transformation
			);			
			
			var fragmentShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
			fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,	
				"mov oc, fc0"							// output colour
			);
			
			_shaderProgram = _context3D.createProgram();
			_shaderProgram.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
			_context3D.setProgram ( _shaderProgram );
			
			_context3D.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0,  // fc0
				Vector.<Number>([ 1.0, 0.0, 0.0, 1.0])	// = Red
			);
			
			// Start animating
			addEventListener(Event.ENTER_FRAME,enterFrame);
		}
		
		
		protected function enterFrame(event:Event):void {
			_context3D.clear(0,0,0);
			// Update Rotation
			_transform.appendRotation( 2, Vector3D.Z_AXIS );
			_context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, _transform, true);
			
			_context3D.drawTriangles( _indexBuffer );
			_context3D.present();
		}
	}
}